* ROBUSTNNESS: PATENT WEIGHTS
* Construct weights for market exposure based on trade rather than patents using EFIGE survey
* Check correlation between weights based on trade and weights based on patents. 

cd "C:\Users\federc\Dropbox\PATSTAT\DATA"
global tmp "D:\TEMP"
global dta "D:\Patstat data"


*------------------------------------------------------------
* Construct weights for market exposure based on sales
*------------------------------------------------------------
use amadeus_efige_list, clear
keep bvd d4
gen wtrade=1-d4/100  // Home share
drop d4
duplicates drop bvd, force
gen group=1
save $tmp/tmp, replace

use amadeus_efige_list, clear
collapse d13* d4 salestheur2008 operatingrevenueturnovertheu2008, by(bvdid nace2 country)
reshape long d13_, i(bvd sales operating d4) j(group)
gen wtrade = d4*d13_/10000
append using $tmp/tmp
collapse (sum) wtrade, by(bvd group)
label define g 1 "EU15" 2 "otherEU" 3 "Euro_noEU" 4 "Chi_Ind" 5 "other_Asia" 6 "US_Ca" 7 "Cent_South_America" 8 "others"
label values group g  
decode group, gen(ctry_group)
drop group
saveold trade_weights2, replace


* ----------------------------------------------
* Construct weights for market exposure based on patents
* Use patent data for 10 years 1998-2008 and group them in the same way as the trade data.
* ----------------------------------------------
use amadeus_efige_list, clear
* Keep patent data for 1998-2008
keep bvdidnumber country nace2 appln_id appln_auth appln_filing_date
gen y = substr(appln_filing_date,1,4)
destring y, replace
drop appln_filing_date
drop if y<1998 | y>2008

* Group patents in same way as trade data
rename appln_auth ctry_code
merge m:1 ctry_code using "$dta/tls801/tls801.dta", keep(match master)
rename ctry_code appln_auth
drop _merge
*tab st3_name
gen EU15=(st3_name=="France" | st3_name=="Germany" | st3_name=="Italy" | st3_name=="Netherlands" ///
	| st3_name=="United Kongdom" | st3_name=="Spain" | st3_name=="Austria" ///
	| st3_name=="European Patent Office (EPO)")
// Note: when the application authority is EPO we assume that the patent was filed
// in at least one EU15 country. There are 291/1137 obs
gen otherEU=0 
// Note: There are no European-non-EU15 countries in the set of appln_auth
gen Euro_noEU=(continent=="Europe" & EU15==0 & otherEU==0)
gen Chi_Ind=(st3_name=="China")
// Note: There is no India
gen other_Asia=(continent=="Asia" & Chi_Ind==0 & st3_name=="Russian Federation")
gen US_Ca=(st3_name=="United States of America" | st3_name=="Canada")
gen Cent_South_America=(continent=="America" & US_Ca==0)
gen others=(EU15==0 & otherEU==0 & Euro_noEU==0 & Chi_Ind==0 & other_Asia==0 ///
	& US_Ca==0 & Cent_South_America==0)
gen ctry_group=""
foreach var of varlist EU15 otherEU Euro_noEU Chi_Ind other_Asia US_Ca Cent_South_America others {
	replace ctry_group="`var'" if `var'==1
}


* Construct weights for market exposure based on patents
by bvdidnumber country appln_auth appln_id , sort: generate nvals = _n == 1
drop if nvals==0
// drop duplicate appln_id for identical combinations of bvdidnumber country appln_auth

collapse (count) p=appln_id, by(bvdidnumber ctry_group)
egen tot = total(p), by(bvd)
gen w = p/tot
label var w "share of patents filed to group"

fillin bvd ctry
replace w=0 if w==.
saveold patent_weights_efige, replace

//Note: appln_auth=="IB" is WIPO applications


* ----------------------------------------------
* Correlation between weights for market exposure based on trade and on patents
* ----------------------------------------------
use patent_weights_efige, clear
merge 1:1 bvdidnumber ctry_group using trade_weights2
replace w=0 if w==.

egen anypatent = total(p), by(bvd)
corr w wtrade if anypatent>0
label var wtrade "Trade weights"
label var w "Patent weights"
lpoly w wtrade if anypatent>0, noscatter ci scheme(lean1) legend(off) title("") 
graph export "../graph/patents_trade_weights_corr.eps", as(eps) preview(on) replace
graph save "../graph/patents_trade_weights_corr.gph", replace
reg w wtrade if anypatent>0



* ----------------------------------------------
* Calculate correlation using weights from Patstat (weightswic5_98to08.dta from weights_wic5.do)
* Use patent data for 10 years 1998-2008 and group them in the same way as the trade data.
* ----------------------------------------------
use weightswic5_98to08, clear
* Aggregate weights in the same way as the trade data
rename appln_auth ctry_code
merge m:1 ctry_code using "$dta/tls801/tls801.dta", keep(match master)
rename ctry_code appln_auth
drop _merge
*tab st3_name
gen EU15=(st3_name=="Belgium" | st3_name=="France" ///
	| st3_name=="Germany" | st3_name=="Italy" | st3_name=="Luxembourg" ///
	| st3_name=="Netherlands" | st3_name=="Denmark" | st3_name=="Ireland" ///
	| st3_name=="United Kongdom" | st3_name=="Greece" | st3_name=="Spain" ///
	| st3_name=="Portugal" | st3_name=="Austria" | st3_name=="Finland" ///
	| st3_name=="Sweden" | st3_name=="European Patent Office (EPO)")
// Note: when the application authority is EPO we assume that the patent was filed
// in at least one EU15 country. 
gen otherEU=( st3_name=="Czech Republik" | st3_name=="Estonia" ///
	| st3_name=="Cyprus" | st3_name=="Latvia" | st3_name=="Lithuania" ///
	| st3_name=="Hungary" | st3_name=="Malta" | st3_name=="Poland" ///
	| st3_name=="Slovak Republic" | st3_name=="Slovenia" | st3_name=="Romania" ///
	| st3_name=="Bulgaria" )
gen Euro_noEU=(continent=="Europe" & EU15==0 & otherEU==0)
gen Chi_Ind=(st3_name=="China" | st3_name=="India")
gen other_Asia=(continent=="Asia" & Chi_Ind==0 & st3_name=="Russian Federation")
gen US_Ca=(st3_name=="United States of America" | st3_name=="Canada")
gen Cent_South_America=(continent=="America" & US_Ca==0)
gen others=(EU15==0 & otherEU==0 & Euro_noEU==0 & Chi_Ind==0 & other_Asia==0 ///
	& US_Ca==0 & Cent_South_America==0)
gen ctry_group=""
foreach var of varlist EU15 otherEU Euro_noEU Chi_Ind other_Asia US_Ca Cent_South_America others {
	replace ctry_group="`var'" if `var'==1
}

collapse (sum) w , by(hrm_l2_id headq ctry_group tot)
fillin hrm ctry_group
replace w=0 if w==.
label var w "Patent weights"

*Merge trade weights
joinby hrm using bvd_hrm_correspondence
merge 1:1 bvdidnumber ctry_group using trade_weights2
replace w=0 if w==.
egen anypatent = max(tot), by(hrm)
replace anypatent=0 if anypatent==.
label var wtrade "Trade weights"

* Correlation
log using ../logs/Appendix_Fig8, replace
di "******* Robustness_patent_weights_Appendix_Fig8 *******"
corr w wtrade if anypatent>0
lpoly w wtrade if anypatent>0, noscatter ci scheme(lean1) legend(off) title("") ///
	xtitle(, size(12pt)) ytitle(, size(12pt)) xlabel(,labsize(12pt)) ylabel(,labsize(12pt))
graph export "../graph/patents_trade_weights_corr_Patstat.eps", as(eps) preview(on) replace
graph save "../graph/patents_trade_weights_corr_Patstat.gph", replace
reg w wtrade if anypatent>0
log close
